package Multithreading.Wait_and_Notify001;

/**
 * ClassName:Cook
 * Package:Multithreading.Wait_and_Notify
 * Description:
 *
 * @Author:ZhangXin
 * @Create2023/3/5-21:47
 * @Version:v1.0
 */
public class Cook extends Thread {
    @Override
    public void run() {
        /*1.循环
         * 2.同步代码块
         * 3.判断共享数据是否到了末尾(到了末尾)
         * 4.判断共享数据没有到末尾(没有到末尾,执行核心逻辑)*/
        while (true) {
            synchronized (Dest.lock) {
                if (Dest.count == 0) {
                    break;
                } else {
                    //判断桌子上是否有食物
                    if (Dest.count == 0) {
                        break;
                    } else {
                        if (Dest.foodFlag == 1) {
                            //如果有,就等待
                            try {
                                Dest.lock.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        } else {
                            //如果没有,就制作食物
                            System.out.println("厨师做了一碗面条");
                            //修改桌子上的食物状态
                            Dest.foodFlag = 1;
                            //叫醒等待的消费者开吃
                            Dest.lock.notifyAll();
                        }
                    }
                }
            }
        }
    }
}
